<?php
/*
	会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=trim($_RQ['name']);
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=trim($_RQ['mobile']);
		$return['params']['mobile']=trim($_RQ['mobile']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_move_agentcar_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['type']==2){
				$number=pdo_getcolumn('jy_move_agentcar_cards',array('mid'=>$item['mid']),'number');
				$item['card_number']=$this->card_number($number);
				$item['ewm']=custom_url('jy_move_agentcar/move//code='.$item['code'],true);
				
			}
			if($item['plate']){
					$item['plate']=json_decode($item['plate'],true);
					$item['plate']=$item['plate']['p'].$item['plate']['c'].$item['plate']['number'];
				}else{
					$item['plate']='未设置';
				}
			$agent=$this->get('agent',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$item['is_agent']=$agent?1:0;
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'会员'),
		),
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'member','title'=>'基本信息','type'=>'member'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'brand','title'=>'汽车品牌','type'=>'string'),
			array('name'=>'plate','title'=>'车牌号','type'=>'string'),
			array('name'=>'card_number','title'=>'挪车卡号','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'ewm','title'=>'二维码','type'=>'ewm'),
			array('name'=>'is_agent','title'=>'是否代理商','type'=>'radio','options'=>'is_agent'),
			
		),'action'=>array(
			array('type'=>'post','op'=>'start','title'=>'解除黑名单','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'off','title'=>'加入黑名单','show'=>array('status'=>0)),
			array('type'=>'modal','op'=>'permission','title'=>'设置代理权限','show'=>array('is_agent'=>1)),
			array('type'=>'href','op'=>'list','action'=>'agent.agent_area','title'=>'代理地区'),
			array('type'=>'href','op'=>'add','action'=>'agent.add','title'=>'添加代理地区'),
			array('type'=>'modal','op'=>'cpassword','action'=>'member.member','plugin'=>'core','title'=>'重置密码'),
			array('type'=>'href','op'=>'agent_detail','title'=>'查看代理商信息','show'=>array('is_agent'=>1)),
		)),
		'search'=>array(
			array('name'=>'type','width'=>15,'title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'name','width'=>15,'title'=>'昵称','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'手机号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['types']=array(
		'0'=>'全部',
		'1'=>'粉丝',
		'2'=>'车主',
	);
	$return['is_agent']=array(
		'0'=>'否',
		'1'=>'是',
	);
	exi($return);
}

//取消禁止
if($_RQ['op']=='start'){
	
	$id=intval($_RQ['id']);
	if($id){
		$this->update('member',array('status'=>0),array('id'=>$id));	
	}
	exi('取消禁止成功！');
}
  
if($_RQ['op']=='off'){
	
	$id=intval($_RQ['id']);
	if($id){
		$this->update('member',array('status'=>1),array('id'=>$id));
	}
	exi('禁止成功！');
}
if($_RQ['op']=='permission'){
	//设置代理商权限
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		if(!$mid){
			exi('请先选择代理商！','error');
		}
		$data=array(
			'add_card'=>intval($post['add_card']),
		);
		$this->update('member',$data,array('mid'=>$mid));
		exi('设置代理商权限成功！');
	}
	$mid=intval($_RQ['mid']);
	if(!$mid){
		exi('请先选择要设置的代理商！','error');
	}
	$item=$this->get('member',array('mid'=>$mid),array('mid','add_card'));
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'switch','name'=>'add_card','title'=>'是否允许创建挪车卡'),
		),
	);	
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
//会员详情
if($_RQ['op']=='detail'){
	exi('功能开发中！','error');
}
//代理信息
if($_RQ['op']=='agent_detail'){
	$mid=intval($_RQ['mid']);
	if(!$mid){
		exi('请先选择代理商！','error','goback');
	}
	
	$item=pdo_get('core_members',array('id'=>$mid),array('avatar','nickname','mobile'));
	
	$commission=pdo_getsum('jy_move_agentcar_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'fee');
	$balance=pdo_getsum('jy_move_agentcar_balance',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'fee');
	$pay=pdo_getsum('jy_move_agentcar_balance',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2),'fee');
	$item['commission']=$commission?:0;
	$item['balance']=$balance?:0;
	$item['pay']=$pay?:0;
	$return['item']=$item;
	$return['htmls']=array(
			'type'=>'look',
			'look'=>array(
				array('type'=>'string','name'=>'nickname','title'=>'昵称'),
				array('type'=>'image','name'=>'avatar','title'=>'头像'),
				array('type'=>'price','name'=>'commission','title'=>'累计佣金'),
				array('type'=>'price','name'=>'balance','title'=>'累计已结算佣金'),
				array('type'=>'price','name'=>'pay','title'=>'累计已打款佣金'),
			),
	);
	exi($return);
}